﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using ZzgkChatRoom.Models;
using ZzgkChatRoom.ViewModels;

namespace ZzgkChatRoom.View
{
    /// <summary>
    /// ChatPage.xaml 的交互逻辑
    /// </summary>
    public partial class ChatPage : Page
    {
        ChatWindowViewModel viewModel;

        public ChatPage(double windowW)
        {
            
            viewModel = new ChatWindowViewModel(windowW);
            DataContext = viewModel;
            InitializeComponent();
            //获取跳转该页面时传递的参数
            
        }

  
        private void ListView_PreviewMouseWheel(object sender, MouseWheelEventArgs e)
        {
            if (!e.Handled)
            {
                // ListView拦截鼠标滚轮事件
                e.Handled = true;

                // 激发一个鼠标滚轮事件，冒泡给外层ListView接收到
                var eventArg = new MouseWheelEventArgs(e.MouseDevice, e.Timestamp, e.Delta);
                eventArg.RoutedEvent = UIElement.MouseWheelEvent;
                eventArg.Source = sender;
                var parent = ((Control)sender).Parent as UIElement;
                parent.RaiseEvent(eventArg);
            }

        }

        private void send_btn_Click(object sender, RoutedEventArgs e)
        {
            string xw = System.Windows.Markup.XamlWriter.Save(MsgInputBox.Document);
            //TextRange textRange = new TextRange(
            //    MessageDocument.ContentStart,
            //    MessageDocument.ContentEnd
            //);
            //viewModel.SendMsg(xw);
            viewModel.SendMsg(MsgInputBox.Document);
            MsgInputBox.Document.Blocks.Clear();
        }

        private void EmojiBtn_Click(object sender, RoutedEventArgs e)
        {
            if (EmojiPop.IsOpen) { 
               EmojiPop.IsOpen = false;
            }else
               EmojiPop.IsOpen = true;
        }

        public void SendBoxShowImg(string key,string path) {
            string ExePath = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
            path = path.Substring(path.IndexOf("/")+1);
            string url = ExePath + path;
            BitmapImage bitmap = new BitmapImage();
            bitmap.BeginInit();
            bitmap.UriSource = new Uri(url, UriKind.RelativeOrAbsolute);
            bitmap.EndInit();
            var container = new InlineUIContainer(new Image { Source = bitmap, Height = 30, Width = 30,Tag = key }, MsgInputBox.CaretPosition);
            MsgInputBox.CaretPosition = container.ElementEnd;
            MsgInputBox.Focus();
            EmojiPop.IsOpen = false;
        }

        private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
         
            ListBox listBox = sender as ListBox;
            if (listBox.SelectedItem == null) return;
            KeyValuePair<string, string> kvp = (KeyValuePair<string, string>)listBox.SelectedItem;
            SendBoxShowImg(kvp.Key,kvp.Value);
        }

        private void Nearlist_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ListView listView = sender as ListView;
            if (listView.SelectedItem == null) return;
            GroupInfo groupInfo = (GroupInfo)listView.SelectedItem;
            if (ChatGrid.Visibility == Visibility.Collapsed)
            {
                ChatGrid.Visibility = Visibility.Visible;
            }
            viewModel.IntoCharRoom(groupInfo);
        }

        private void winMin(object sender, RoutedEventArgs e)
        {
            var window = (MainWindow)Window.GetWindow(this);
            window.WindowState = WindowState.Minimized;
        }

        private void winMax(object sender, RoutedEventArgs e) {
            var window = (MainWindow)Window.GetWindow(this);
            window.WindowState = WindowState.Maximized;
        }
    }
}
